import xadmin

from apps.courses.models import Course, Video, CourseResource, Lesson, CourseTag,BannerCoures


class GlobalSettings(object):
    site_title = "慕学教程后台管理系统"
    site_footer = "慕学在线网"
    # menu_style = "accordion"

class BaseStettings(object):
    enable_themes = True
    use_bootswatch = True

class CourseAdmin(object):
    list_display = ["name", "desc", "show_image", "detail", "degree", "learn_times", "students",  'is_classics']
    search_fields = ["name", "desc", "detail", "degree","students",  'is_classics']
    list_filter = ["name","teacher__name", "desc", "detail", "degree", "learn_times", "students", 'is_classics']
    list_editable = ["name", "desc", "degree", 'is_classics']
    # 只读
    readonly_fields = ["click_nums", "fav_nums", "students", "add_time"]
    model_icon = 'fa fa-window-maximize'
    style_fields = {"detail": "ueditor"}
    def queryset(self):
        qs = super().queryset()
        if not self.request.user.is_superuser:
            qs = qs.filter(teacher=self.request.user.teacher)
        return qs

class BannerCourseAdmin(object):
    list_display = ["name", "desc", "show_image", "detail", "degree", "learn_times", "students",  'is_classics']
    search_fields = ["name", "desc", "detail", "degree","students",  'is_classics']
    list_filter = ["name","teacher__name", "desc", "detail", "degree", "learn_times", "students", 'is_classics']
    list_editable = ["name", "desc", "degree", 'is_classics']
    readonly_fields = ["click_nums", "fav_nums", "students", "add_time"]
    model_icon = 'fa fa-window-restore'

    def queryset(self):
        qs = super().queryset()
        qs = qs.filter(is_banner=True)
        return qs


class LessonAdmin(object):
    list_display = ["course", "name", "add_time"]
    search_fields = ["course", "name"]
    list_filter = ["course__name", "name", "add_time"]
    list_editable = ["course", "name"]
    readonly_fields = ["add_time"]
    model_icon = 'fa fa-tasks'

class VideoAdmin(object):
    list_display = ["id", "name", "lesson", "learn_time", "url"]
    search_fields = ["id", "name", "lesson", "url"]
    list_filter = ["id", "name", "lesson", "learn_time"]
    list_editable = ["name", "lesson", "url"]
    readonly_fields = ["add_time"]
    model_icon = 'fa fa-play-circle-o'
class CourseResourceAdmin(object):
    list_display = ["id", "name", "course", "file"]
    search_fields = ["id", "name", "course"]
    list_filter = ["id", "name", "course"]
    list_editable = ["name", "course", "file"]
    readonly_fields = ["add_time"]
    model_icon = 'fa fa-file'
class CourseTagAdmin(object):
    list_display = ["id", "course", "tag", "add_time"]
    search_fields = ["id", "course", "tag"]
    list_filter = ["id", "course", "tag"]
    list_editable = ["id", "course", "tag"]
    readonly_fields = ["add_time"]
    model_icon = 'fa fa-tags'
xadmin.site.register(BannerCoures, BannerCourseAdmin)
xadmin.site.register(Course, CourseAdmin)
xadmin.site.register(Lesson, LessonAdmin)
xadmin.site.register(Video, VideoAdmin)
xadmin.site.register(CourseResource, CourseResourceAdmin)
xadmin.site.register(CourseTag, CourseTagAdmin)
xadmin.site.register(xadmin.views.CommAdminView, GlobalSettings)
xadmin.site.register(xadmin.views.BaseAdminView, BaseStettings)